//
//  ZZAORBView.m
//  ZZBlue
//
//  Created by stonesMonkey on 2017/9/26.
//  Copyright © 2017年 stonesMonkey. All rights reserved.
//

#import "ZZAORBView.h"

@interface  ZZAORBView()

@property (strong, nonatomic)  UIView *borderView;
@property (strong, nonatomic)  UIButton *ABtn;

@property (strong, nonatomic)  UIButton *BBtn;


@end

@implementation ZZAORBView


- (void)setupSubView {

    self.borderView = [[UIView alloc] init];
    self.borderView.frame = self.bounds;
    
    self.ABtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.ABtn.frame = CGRectMake(0, 0, 95, 33);
    self.ABtn.tag = 1000;
    [self.ABtn setTitle:@"A" forState:UIControlStateNormal];
    self.ABtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [self.ABtn addTarget:self action:@selector(btnDidClick:) forControlEvents:UIControlEventTouchDown];
    
    
    self.BBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.BBtn.frame = CGRectMake(95, 0, 95, 33);
    self.BBtn.tag = 1001;
    
    [self.BBtn setTitle:@"B" forState:UIControlStateNormal];
    [self.BBtn addTarget:self action:@selector(btnDidClick:) forControlEvents:UIControlEventTouchDown];
    
    [self addSubview:self.borderView];
    [self addSubview:self.ABtn];
    [self addSubview:self.BBtn];
    
    self.borderView.layer.cornerRadius = 16.5;
    self.borderView.layer.borderWidth = 2;
    self.borderView.layer.borderColor = [UIColor zColorWithRed:59 green:16 blue:22].CGColor;
    
    self.ABtn.layer.borderWidth = 2;
    self.BBtn.layer.borderWidth = 2;
    self.ABtn.layer.cornerRadius = 16.5;
    self.BBtn.layer.cornerRadius = 16.5;
    self.ABtn.layer.borderColor = [UIColor redColor].CGColor;
    self.BBtn.layer.borderColor = [UIColor clearColor].CGColor;
    self.selectTag = 1000;
    self.ABtn.selected = YES;
}


- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        
        [self setupSubView];
    }
    return self;
}

- (IBAction)btnDidClick:(UIButton *)sender {
    
    NSInteger tag = sender.tag;
    if (self.AorBClick) {
        
        self.AorBClick(sender.tag);
    }
    if (self.selectTag == sender.tag) {
        
        return;
    }
    
    self.selectTag = sender.tag;
    
    if (self.selectTag == 1000) { // A
        
        self.ABtn.selected = YES;
        self.BBtn.selected = NO;
        self.ABtn.layer.borderColor = [UIColor redColor].CGColor;
        self.BBtn.layer.borderColor = [UIColor clearColor].CGColor;
        
    } else { // B
        self.ABtn.selected = NO;
        self.BBtn.selected = YES;
        self.BBtn.layer.borderColor = [UIColor redColor].CGColor;
        self.ABtn.layer.borderColor = [UIColor clearColor].CGColor;
    }
}

- (void)selelctIndex:(NSInteger)index {
    
    if (index == 1000) {
        
        [self btnDidClick:self.ABtn];
    } else {
        
        [self btnDidClick:self.BBtn];
    }
}

@end
